package com.jdjw.modules.thirdpost.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jdjw.common.ResponseResult;
import com.jdjw.modules.abuttingjoint.entity.DwdGcFkxxJiande;
import com.jdjw.modules.abuttingjoint.mapper.DwdGcFkxxJiandeMapper;
import com.jdjw.modules.thirdpost.param.ProjectCompanyParam;
import com.jdjw.modules.thirdpost.param.ProjectPayParam;
import com.jdjw.modules.thirdpost.param.ThirdPostInParm;
import com.jdjw.modules.thirdpost.result.ProjectCompanyResult;
import com.jdjw.modules.thirdpost.result.ProjectPayResult;
import com.jdjw.utils.CommonUtil;
import com.jdjw.utils.ConstantUtil;
import com.jdjw.utils.DateUtil;
import com.jdjw.utils.HttpRequestUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 工程项目支付，工程单位，工程单位人员 推送数据
 */
@Service("gczf")
@Slf4j
public class ProjectPayPersonnelServiceImpl extends ServiceImpl<DwdGcFkxxJiandeMapper, DwdGcFkxxJiande>
        implements IService<DwdGcFkxxJiande> {

    @Autowired
    private static DwdGcFkxxJiandeMapper gcFkxxJiandeMapper;

    @Autowired
    static ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl;

    @Resource
    public void setThirdPostExceptionServiceImpl(ThirdPostExceptionServiceImpl thirdPostExceptionServiceImpl) {
        ProjectPayPersonnelServiceImpl.thirdPostExceptionServiceImpl = thirdPostExceptionServiceImpl;
    }

    @Resource
    public void setGcJbxxJiandeMapper(DwdGcFkxxJiandeMapper gcFkxxJiandeMapper) {
        ProjectPayPersonnelServiceImpl.gcFkxxJiandeMapper = gcFkxxJiandeMapper;
    }

    /**
     * 工程项目支付
     */
    public static void doPostPay(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 7;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 7;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 7;
        }
        try {
            //查询
            List<ProjectPayResult> all = gcFkxxJiandeMapper.getPayListAll(thirdPostInParm);
            if (all == null || all.size() < 1) {
                log.info("工程基础表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<ProjectPayResult>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<ProjectPayResult> obj : fgList) {
                    sendDataPay((List<ProjectPayResult>) obj, url);
                }
            } else {
                sendDataPay(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工程项目支付组装数据
     */
    private static void sendDataPay(List<ProjectPayResult> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (ProjectPayResult a : all) {
            //组装数据
            ProjectPayParam param = new ProjectPayParam();
            BeanUtils.copyProperties(a, param);
            param.setId(a.getId());
            param.setBiz_id(a.getBizId());
            param.setDel_flag(a.getDelFlag());
            param.setParnet_id(a.getParnetId());
            param.setPzh(a.getPzh());//太长了，对面接收不了
            //补0至16位
            param.setCwhxzqh(CommonUtil.addZeroForNum(param.getCwhxzqh(), 16));
            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("工程基础表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostPay", "工程基础数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("工程基础表推送失败", result);
            }
        } else {
            log.info("工程基础表推送失败", "第三方返回空");
        }
    }

    /**
     * 工程项目单位
     */
    public static void doPostCompany(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 5;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 5;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 5;
        }
        try {
            //查询
            List<ProjectCompanyResult> all = gcFkxxJiandeMapper.getCompanyListAll(thirdPostInParm);
            if (all == null || all.size() < 1) {
                log.info("工程项目单位表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<ProjectCompanyResult>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<ProjectCompanyResult> obj : fgList) {
                    sendDataCompany((List<ProjectCompanyResult>) obj, url);
                }
            } else {
                sendDataCompany(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工程项目单位组装数据
     */
    private static void sendDataCompany(List<ProjectCompanyResult> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (ProjectCompanyResult a : all) {
            //组装数据
            ProjectCompanyParam param = new ProjectCompanyParam();
            BeanUtils.copyProperties(a, param);
            param.setBiz_id(a.getBizId());
            param.setDel_flag(a.getDelFlag());
            param.setParent_id(a.getParentId());
            //补0至16位
            param.setCwhxzqh(CommonUtil.addZeroForNum(param.getCwhxzqh(), 16));
            //转化为json字符串
            String jsonStr = JSONObject.toJSONString(param);
            //转化为json对象
            list.add(JSONObject.parseObject(jsonStr));
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果：" + jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("工程单位表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostCompany", "工程单位表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("工程单位表推送失败", result);
            }
        } else {
            log.info("工程单位表推送失败", "第三方返回空");
        }
    }

    /**
     * 工程项目单位人员
     */
    public static void doPostPersonnel(ThirdPostInParm thirdPostInParm) {
        String url = "";
        //全量
        if (ConstantUtil.ALL_TYPE.equals(thirdPostInParm.getMethodType())) {
            url = ConstantUtil.ALL_THIRD_POST + 6;
        } else if (ConstantUtil.ADD_TYPE.equals(thirdPostInParm.getMethodType())) {
            //增量必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.ADD_THIRD_POST + 6;
        } else {
            //更新必须有时间
            if (StringUtils.isEmpty(thirdPostInParm.getStartTime()) &&
                    StringUtils.isEmpty(thirdPostInParm.getEndTime())) {
                return;
            }
            url = ConstantUtil.UPDATE_THIRD_POST + 6;
        }
        try {
            //查询
            List<Map> all = gcFkxxJiandeMapper.getPersonnelListAll(thirdPostInParm);
            if (all == null || all.size() < 1) {
                log.info("工程单位人员表数据库查询数据为0条");
                return;
            }
            if (all.size() > 3000) {
                Collection<Collection<Map>> fgList = CommonUtil.subCollection(all, 2900);
                for (Collection<Map> obj : fgList) {
                    sendDataPersonnel((List<Map>) obj, url);
                }
            } else {
                sendDataPersonnel(all, url);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 工程项目单位人员组装数据
     */
    private static void sendDataPersonnel(List<Map> all, String url) {
        ResponseResult result = new ResponseResult();
        ArrayList list = new ArrayList();
        for (Map map : all) {
            //组装数据
            map.put("cwhxzqh", CommonUtil.addZeroForNum(String.valueOf(map.get("cwhxzqh")), 16));
            list.add(map);
        }
        //发送(post)
        net.sf.json.JSONObject jsonObject = HttpRequestUtil.getHttpResponseJsonAllSSL(url, list);
        System.out.println("结果："+jsonObject);
        if (jsonObject != null) {
            if ("200".equals(jsonObject.get("code"))) {
                result.setCode(200);
                result.setMsg("SUCCESS");
                result.setData("推送完成");
                log.info("工程单位人员表推送成功"+list.size(), result);
            } else {
                //记录日志
                thirdPostExceptionServiceImpl.insertExceptionData(
                        "doPostPersonnel", "工程单位人员表数据",
                        jsonObject.getString("message"),
                        DateUtil.formatDate(DateUtil.FORMAT_YMD, new Date().getTime()));
                result.setCode(500);
                result.setMsg("ERROR");
                result.setData(jsonObject.getString("message"));
                log.info("工程单位人员表推送失败", result);
            }
        } else {
            log.info("工程单位人员表推送失败", "第三方返回空");
        }
    }
}
